package com.baitiaojun.tenant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baitiaojun.common.enums.impl.TenantStatusEnums;
import com.baitiaojun.common.utils.date.DateTimeUtils;
import com.baitiaojun.common.utils.string.StringUtils;
import com.baitiaojun.tenant.dto.MasterTenantDTO;
import com.baitiaojun.tenant.dto.TenantDTO;
import com.baitiaojun.tenant.mapper.MasterTenantMapper;
import com.baitiaojun.tenant.pojo.MasterTenantDO;
import com.baitiaojun.tenant.service.MasterTenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("masterTenantServiceImpl")
public class MasterTenantServiceImpl extends ServiceImpl<MasterTenantMapper, MasterTenantDO> implements MasterTenantService {

    @Autowired
    private MasterTenantMapper masterTenantMapper;

    @Override
    public MasterTenantDTO queryTenant(String tenant) {
        MasterTenantDO masterTenantDO = masterTenantMapper.selectMasterTenant(tenant);
        if (masterTenantDO == null) {
            return null;
        }
        MasterTenantDTO masterTenantDTO = new MasterTenantDTO();
        BeanUtils.copyProperties(masterTenantDO, masterTenantDTO);
        return masterTenantDTO;
    }

    @Override
    public boolean createTenant(MasterTenantDTO masterTenantDTO) {
        MasterTenantDO masterTenantDO = BeanUtil.copyProperties(masterTenantDTO, MasterTenantDO.class);
        masterTenantDO.setStatus(String.valueOf(TenantStatusEnums.NORMAL.getCode()));
        masterTenantDO.setCreateTime(DateTimeUtils.parseDate(DateTimeUtils.getDateTime()));
        int insert = masterTenantMapper.insert(masterTenantDO);
        return insert == 1;
    }
}
